package com.maxsalad.eleme.service;

import com.maxsalad.common.util.PropertiesUtil;
import com.maxsalad.eleme.util.SignUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by 2014-400 on 2016-03-07.
 */
public abstract class BaseService {

    protected String getBaseUrl() throws Exception {
        return PropertiesUtil.getValue("ELEME_URL");
    }

    protected String getConsumerKey() throws Exception {
        return PropertiesUtil.getValue("ELEME_CONSUMER_KEY");
    }

    protected String getConsumerSecret() throws Exception {
        return PropertiesUtil.getValue("ELEME_CONSUMER_SECRET");
    }

    protected String getRestaurantId() throws Exception {
        return PropertiesUtil.getValue("ELEME_RESTAURANT_ID");
    }

    protected String getRestaurantName() throws Exception {
        return PropertiesUtil.getValue("ELEME_RESTAURANT_NAME");
    }

    /**
     * 获取系统参数
     *
     * @return
     * @throws Exception
     */
    public Map<String, String> getSystemParam() throws Exception {
        Map params = new HashMap<String, String>();
        params.put("consumer_key", getConsumerKey());
        params.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000L));

        return params;
    }

    /**
     * 获取签名
     *
     * @param pathUrl
     * @param params
     * @return
     * @throws Exception
     */
    protected String getSign(String pathUrl, Map<String, String> params) throws Exception {
        String consumerSecret = getConsumerSecret();
        return SignUtil.getSign(pathUrl, params, consumerSecret);
    }

    /**
     * 拼接参数键值对
     *
     * @param params
     * @return
     * @throws UnsupportedEncodingException
     */
    public String concatParams(Map<String, String> params) throws UnsupportedEncodingException {
        Object[] key_arr = params.keySet().toArray();
        Arrays.sort(key_arr);

        StringBuffer buffer = new StringBuffer();
        for (Object key : key_arr) {
            String val = params.get(key);
            buffer.append("&" + key + "=" + URLEncoder.encode(val, "UTF-8"));
        }

        return buffer.toString().replaceFirst("&", "");
    }


    /**
     * 返回经过签名处理的url
     *
     * @param pathUrl 不带参数的url
     * @param params  其他条件参数,如:name=张三
     * @param data    body体数据
     * @return
     * @throws Exception
     */
    public String getRequestUrl(String pathUrl, Map<String, String> params, Map<String, String> data) throws Exception {
        Map<String, String> systemParams = getSystemParam();

        if (params != null && params.size() > 0) {
            for (String key : params.keySet()) {
                systemParams.put(key, params.get(key));
            }
        }

        Map<String, String> temp = new HashMap<String, String>(systemParams);
        if (data != null && data.size() > 0) {
            for (String key : data.keySet()) {
                temp.put(key, data.get(key));
            }
        }

        String sign = getSign(pathUrl, temp);
        systemParams.put("sig", sign);


        return pathUrl += "?" + concatParams(systemParams);
    }
}
